package com.itheima.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;


@RequestMapping("/employee")
@RestController
@Slf4j
public class EnployeeConterllor {
    @Autowired
    private EmployeeService employeeService;

    /*
     * 员工登录*/
    @PostMapping("/login")
    public R<Employee> getLogIn(HttpServletRequest request, @RequestBody Employee employee) {

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //根据用户提交的用户名查询数据库
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);
        //如果没有查询到返回登录失败
        if (emp == null) {
            return R.error("登录失败");
        }
        //密码比对
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }
        //查看员工状态
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        //登陆成功，将员工id存入session
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    //退出登录
    @PostMapping("/logout")
    public R<String> tologout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /*
     * 分页+模糊查询*/
    @GetMapping("/page")
    public R<Page> selectPage(int page, int pageSize, String name) {
        Page<Employee> pages = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Employee::getUsername, name);
        wrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pages, wrapper);
        return R.success(pages);
    }

    /*
     * 添加员工数据*/
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        //employeeService.save(employee);
        log.info("新增员工，员工信息：{}", employee.toString());
        //设置初始密码，
        /*employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //获得当前登录用户的id
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/

        employeeService.save(employee);
        return R.success("新增员工成功");

    }

   /*
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("根据id查询员工信息...");
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }
    /*
    *根据id修改员工信息 */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){

        /*Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);*/
        employeeService.updateById(employee);

        return R.success("修改员工信息成功");
    }



}
